home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / passthru.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  2.5 KB  |  139 lines

  1. /*
  2.  * A transparent "pass-thru" mode, designed to allow binary transfers
  3.  * between 3 machines (with the middle machine in the pass-thru mode).
  4.  * A non-zero return code means the input routine should be restarted.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <signal.h>
  9. #include <curses.h>
  10. #include "config.h"
  11. #include "misc.h"
  12.  
  13. #ifdef BSD
  14. #include <setjmp.h>
  15. jmp_buf cp_buf;
  16. #endif /* BSD */
  17.  
  18. int
  19. pass_thru()
  20. {
  21.     extern int fd;
  22.     WINDOW *pt_win, *newwin();
  23.     int num;
  24.     void error_win();
  25.     static void cpio();
  26.  
  27.     pt_win = newwin(5, 70, 5, 5);
  28.  
  29.     mvwaddstr(pt_win, 2, 4, "Enter the expiration time (5-60 sec): ");
  30.     box(pt_win, VERT, HORZ);
  31.  
  32.     mvwattrstr(pt_win, 0, 3, A_BOLD, " Pass Through Mode ");
  33.     wmove(pt_win, 2, 43);
  34.     wrefresh(pt_win);
  35.                     /* get the answer */
  36.     while ((num = get_num(pt_win, 2)) != -1) {
  37.                     /* out of bounds */
  38.         if (num < 5 || num > 60) {
  39.             beep();
  40.             clear_line(pt_win, 2, 43, TRUE);
  41.             wmove(pt_win, 2, 43);
  42.             wrefresh(pt_win);
  43.         }
  44.         else {
  45.             werase(pt_win);
  46.             wrefresh(pt_win);
  47.             delwin(pt_win);
  48.  
  49.             if (fd == -1) {
  50.                 error_win(0, "Not currently connected to any host", "");
  51.                 return(0);
  52.             }
  53.  
  54.             touchwin(stdscr);
  55.             refresh();
  56.  
  57.             cpio((unsigned int) num);
  58.             return(1);
  59.         }
  60.     }
  61.     if (fd == -1) {
  62.         werase(pt_win);
  63.         wrefresh(pt_win);
  64.     }
  65.     delwin(pt_win);
  66.     return(0);
  67. }
  68.  
  69. /*
  70.  * Copy the stdin to the TTYout and copy the TTYin to the stdout.  Uses
  71.  * multi character reads.  I'm not too concerned about the excess baggage
  72.  * caused by the entire image being forked... this feature won't be used
  73.  * that often.
  74.  */
  75.  
  76. static int cp_flag;
  77.  
  78. static void
  79. cpio(num)
  80. unsigned int num;
  81. {
  82.     extern int fd;
  83.     int cpid, n;
  84.     static int cp_force();
  85.     char buf[CLIST_SIZ];
  86.     unsigned int alarm(), sleep();
  87.     void line_set(), xmodem_mode(), input_off();
  88.  
  89.                     /* out of curses mode */
  90.     resetterm();
  91.  
  92.     input_off();
  93.     xmodem_mode(0);
  94.     xmodem_mode(fd);
  95.  
  96.                     /* copy the TTYin to stdout */
  97.     if (!(cpid = fork())) {
  98.         while (1) {
  99.             n = read(fd, buf, CLIST_SIZ);
  100.             write(1, buf, n);
  101.         }
  102.     }
  103.  
  104.     cp_flag = 0;
  105.     signal(SIGALRM, (SIG_TYPE(*) ()) cp_force);
  106.                     /* copy the stdin to TTYout */
  107.     while (1) {
  108.         alarm(num);
  109. #ifdef BSD
  110.         if (setjmp(cp_buf))
  111.             break;
  112. #endif /* BSD */
  113.         n = read(0, buf, CLIST_SIZ);
  114.         if (cp_flag)
  115.             break;
  116.         write(fd, buf, n);
  117.     }
  118.     kill(cpid, SIGKILL);
  119.                     /* back to curses mode */
  120.     sleep(1);
  121.     fixterm();
  122.     beep();
  123.     line_set();
  124.     clearok(curscr, TRUE);
  125.     return;
  126. }
  127.  
  128. /* ARGSUSED */
  129. static int
  130. cp_force(dummy)
  131. {
  132. #ifdef BSD
  133.     longjmp(cp_buf, 1);
  134. #else /* BSD */
  135.     signal(SIGALRM, (SIG_TYPE(*) ()) cp_force);
  136.     cp_flag = 1;
  137. #endif /* BSD */
  138. }
  139.